package com.hzwq.appkeepalive

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.*
import android.provider.Settings
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.hzwq.Utils.OpenHotSpot
import com.hzwq.service.BackGroundService
import com.hzwq.service.VoiceFloatingService
import kotlinx.android.synthetic.main.activity_main.*

/**
 * 需要两个就行了。  电池白名单和前台服务
 */
class MainActivity : AppCompatActivity() {
    var index = 0

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        handlers.sendEmptyMessageDelayed(0, 10)
        //开启服务  把服务放在前台防止进程被系统杀死
        val forgroundService = Intent(this, BackGroundService::class.java)
        startService(forgroundService)
        if (isIgnoringBatteryOptimizations()) {
            Toast.makeText(this, "在白名单", Toast.LENGTH_SHORT).show()
        } else {
            requestIgnoreBatteryOptimizations()
        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            OpenHotSpot.open()
//        }
//        OpenHotSpot.setWifiAp("HZWQ","12345678")
    }

    override fun onResume() {
        super.onResume()
        showFloat()
    }


   fun showFloat(){
        //Android 6.0 以下无需获取权限，可直接展示悬浮窗
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //判断是否拥有悬浮窗权限，无则跳转悬浮窗权限授权页面
            if (Settings.canDrawOverlays(this)) {
                showFloatingView()
            } else {
                //跳转悬浮窗权限授权页面
                startActivity(
                        Intent(
                                Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                                Uri.parse("package:$packageName")
                        )
                )
            }
        } else {
            showFloatingView()
        }
    }
    /**
     * 显示悬浮窗
     */
    private fun showFloatingView() {
        if (VoiceFloatingService.isStart) {
            //通知显示悬浮窗
            LocalBroadcastManager.getInstance(this).sendBroadcast(Intent(VoiceFloatingService.ACTION_SHOW_FLOATING))
        } else {
            //启动悬浮窗管理服务
            startService(Intent(this, VoiceFloatingService::class.java))
        }
    }
    @RequiresApi(api = Build.VERSION_CODES.M)
    private fun isIgnoringBatteryOptimizations(): Boolean {
        var isIgnoring = false
        val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
        if (powerManager != null) {
            isIgnoring = powerManager.isIgnoringBatteryOptimizations(packageName)
        }
        return isIgnoring
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private fun requestIgnoreBatteryOptimizations() {
        try {
            var intent = Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:$packageName"));
            startActivity(intent);
        } catch (e: Exception) {
            e.printStackTrace();
        }
    }

    /**
     * 定时打印日志
     */
    internal var handlers: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            index++
            Log.d("程序保活===", index.toString())
            //1分钟一次
            toInfo()
        }
    }

    private fun toInfo() {
        //5分钟一次
        handlers.sendEmptyMessageDelayed(0, 60 * 1000)
    }

}